var express = require('express');
var fs = require('fs');
var toPdf = require("office-to-pdf");
var router = express.Router();

/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', { title: 'Express' });
});

// 转换word为PDF
function wordToPdf() {
  // let wordFile = __dirname + "/test.doc";
  let wordFile = "./public/excel/知识检索.docx"
  return fs.readFile(wordFile, function (err, result) {
    if (err) { console.log(err); } else {
      toPdf(result).then((pdfBuffer) => {
        fs.writeFileSync("./public/pdf/test.pdf", pdfBuffer);
        console.log('成功生成PDF文件')
        // sendFileToServer('./pdf/test.pdf', '/sftp/pdf/test.pdf')
      }, (err) => { console.log(err); });
    }
  });
}

router.post('/getData', function (req, res, next) {
  res.send({
    code:200
  })
  fs.readFile("./public/excel/a.json", (err, data) => {
    let jn1 = [{
      c: 555
    }]
    let jn=JSON.stringify(jn1)
    fs.writeFile("./public/excel/a.json",jn,(err,data)=>{
      res.send({
        code:200
      })
    })
  })
});

router.post('/dealData', function (req, res, next) {
  // for(let i=0;i<100000;i++){
  //   console.log(i);
  // }
  res.send({
    code:req.body.index
  })
});

// wordToPdf();
module.exports = router;
